<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/12/19
 * Time: 15:42
 */

namespace app\common;


use PHPMailer\PHPMailer\PHPMailer;

class email
{
    //发送邮箱验证码
    public static function send($data,$receive,$send)
    {
        $toemail = $receive['email'];//定义收件人的邮箱

        $mail = new PHPMailer();

        $mail->isSMTP();// 使用SMTP服务
        $mail->CharSet = "utf8";// 编码格式为utf8，不设置编码的话，中文会出现乱码
        $mail->Host = "smtp.163.com";// 发送方的SMTP服务器地址
        $mail->SMTPAuth = true;// 是否使用身份验证
        $mail->Username = $send['send'];
        //发送方的163邮箱用户名，就是你申请163的SMTP服务使用的163邮箱
        $mail->Password = $send['secret'];
        //发送方的邮箱密码，注意用163邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码！
        $mail->SMTPSecure = "ssl";
        //使用ssl协议方式
        $mail->Port = 994;// 163邮箱的ssl协议方式端口号是465/994
        $title = 'Email From CN Sales Lead';
        $mail->setFrom($send['send'],'shopeeSFDC');// 设置发件人信息，如邮件格式说明中的发件人，这里会显示为Mailer(xxxx@163.com），Mailer是当做名字显示
        $mail->addAddress($toemail);// 设置收件人信息，如邮件格式说明中的收件人，这里会显示为Liang(yyyy@163.com)
//        $mail->addReplyTo($config['email'],'Reply:'.$data['title']);// 设置回复人信息，指的是收件人收到邮件后，如果要回复，回复邮件将发送到的邮箱地址
        //$mail->addCC("xxx@163.com");// 设置邮件抄送人，可以只写地址，上述的设置也可以只写地址(这个人也能收到邮件)
        //$mail->addBCC("xxx@163.com");// 设置秘密抄送人(这个人也能收到邮件)
        //$mail->addAttachment("bug0.jpg");// 添加附件

        $html = "联系人：".$data['linkname']."<br/>";
        $html .= "联系手机：".$data['tel']."<br/>";
        $html .= "邮箱：".$data['email']."<br/>";
        $html .= "公司名称：".$data['company']."<br/>";
        $html .= "QQ：".$data['qq']."<br/>";
        $html .= "公司地址：".$data['province'].$data['city']."<br/>";
        $html .= "员工总数：".$data['employee_num']."<br/>";
        $html .= "营业执照编号：".$data['license']."<br/>";
        $html .= "主要品类：".$data['category']."<br/>";
        $html .= "Listing总数：".$data['listing']."<br/>";
        $html .= "其他经营平台：".$data['other_plat']."<br/>";
        $html .= "备注：".$data['remark'];

        $mail->isHTML(true);
        $mail->Subject = $title;// 邮件标题
        $mail->Body = $html;// 邮件正文
        //$mail->AltBody = "This is the plain text纯文本";// 这个是设置纯文本方式显示的正文内容，如果不支持Html方式，就会用到这个，基本无用

        if(!$mail->send()){// 发送邮件
            return ['code'=>0,'msg'=>$mail->ErrorInfo];
        }else{
            return ['code'=>1,'msg'=>'发送成功'];
        }


    }

    /**
     * @param $order
     * 退款向保险公司发送短信
     */
    public static function refund_send_email($order)
    {
        $insurance = unserialize($order['insurance_info']);
        $passengers = unserialize($order['passengers']);
        foreach ($insurance as $key=>$value)
        {
            $html = "<p>动途旅行退订".$order['quantity']."份".$value['name']."</p>";
            $html .= "<p>订单号:".$order['member_order_number']."</p>";
            $html .= "<p>下单时间:".date('Y-m-d H:i',$order['create_time'])."</p>";
            $html .= "<p>保额单价:".$value['price']." 元</p>";
            $html .= "<p>数量:".$order['quantity']." 份</p>";
            $html .= "<p>总金额:".$order['quantity']*$value['price']." 元</p>";
            $html .= "<p>乘客信息如下:</p>";
            foreach ($passengers as $k=>$v)
            {
                $html .= "<p>姓名:".$v['realname'].",身份证号:".$v['id_number']."</p>";
            }
            $email['email'] = $value['email'];
            $email['company_name'] = $value['company_name'];
            $email['title'] ="√动途旅行退订".$order['quantity']."份".$value['name'].',订单号'.$order['member_order_number'];
            $email['content'] = $html;
            self::send($email);
        }

    }

    /**
     * @param $order
     * 付款向保险公司发送短信
     */
    public static function pay_send_email($order)
    {
        $insurance = unserialize($order['insurance_info']);
        $passengers = unserialize($order['passengers']);
        foreach ($insurance as $key=>$value)
        {
            $html = "<p>动途旅行刚刚购买".$order['quantity']."份".$value['name']."</p>";
            $html .= "<p>订单号:".$order['member_order_number']."</p>";
            $html .= "<p>下单时间:".date('Y-m-d H:i',$order['create_time'])."</p>";
            $html .= "<p>保额单价:".$value['price']." 元</p>";
            $html .= "<p>数量:".$order['quantity']." 份</p>";
            $html .= "<p>总金额:".$order['quantity']*$value['price']." 元</p>";
            $html .= "<p>乘客信息如下:</p>";
            foreach ($passengers as $k=>$v)
            {
                $html .= "<p>姓名:".$v['realname'].",身份证号:".$v['id_number']."</p>";
            }
            $email['email'] = $value['email'];
            $email['company_name'] = $value['company_name'];
            $email['title'] = "x动途旅行购买".$order['quantity']."份".$value['name'].',订单号'.$order['member_order_number'];
            $email['content'] = $html;
            self::send($email);
        }
    }
}